package com.xdf.ecommerce.entity;

import com.alibaba.fastjson.JSON;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;
import com.xdf.ecommerce.constant.BrandCategory;
import com.xdf.ecommerce.constant.GoodsCategory;
import com.xdf.ecommerce.constant.GoodsStatus;
import com.xdf.ecommerce.goods.GoodsInfo;
import com.xdf.ecommerce.goods.SimpleGoodsInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Convert;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @author chanchaw
 * @create 2025-10-27 21:16
 */
@Table("t_ecommerce_goods")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class EcommerceGoods implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id(keyType = KeyType.Auto)
    private Long id;
    @Column("goods_category")
    @Convert(converter = GoodsCategory.class)
    private String goodsCategory;
    @Column("brand_category")
    @Convert(converter = BrandCategory.class)
    private String brandCategory;
    private String goodsName;
    private String goodsPic;
    private String goodsDescription;
    @Column("goods_status")
    @Convert(converter = GoodsStatus.class)
    private Integer goodsStatus;
    private Integer price;
    private Long supply;
    private Long inventory;
    private String goodsProperty;
    private LocalDateTime createTime;
    private String createUser;
    private LocalDateTime updateTime;
    private String updateUser;
    private Integer isDelete;

    /**
     * 将类型 GoodsInfo 对象转换为 EcommerceGoods
     * @param goodsInfo
     * @return
     */
    public static EcommerceGoods to(GoodsInfo goodsInfo){
        EcommerceGoods ret = new EcommerceGoods();
        ret.setId(goodsInfo.getId());
        ret.setGoodsName(goodsInfo.getGoodsName());
        ret.setGoodsCategory(goodsInfo.getGoodsCategory());
        ret.setBrandCategory(goodsInfo.getBrandCategory());
        ret.setGoodsPic(goodsInfo.getGoodsPic());
        ret.setGoodsDescription(goodsInfo.getGoodsDescription());
        ret.setGoodsStatus(goodsInfo.getGoodsStatus());
        ret.setPrice(goodsInfo.getPrice());
        ret.setGoodsProperty(JSON.toJSONString(goodsInfo.getGoodsProperty()));
        ret.setSupply(goodsInfo.getSupply());
        ret.setInventory(goodsInfo.getInventory());
        ret.setCreateTime(goodsInfo.getCreateTime());
        ret.setCreateUser(goodsInfo.getCreateUser());
        ret.setUpdateTime(goodsInfo.getUpdateTime());
        ret.setUpdateUser(goodsInfo.getUpdateUser());
        return ret;
    }

    /**
     * 将 EcommerceGoods 转换为 GoodsInfo 对象
     * @param g
     * @return
     */
    public static GoodsInfo toGoodsInfo(EcommerceGoods g){
        GoodsInfo ret = new GoodsInfo();
        ret.setId(g.getId());
        ret.setGoodsName(g.getGoodsName());
        ret.setGoodsCategory(g.getGoodsCategory());
        ret.setBrandCategory(g.getBrandCategory());
        ret.setGoodsPic(g.getGoodsPic());
        ret.setGoodsDescription(g.getGoodsDescription());
        ret.setGoodsStatus(g.getGoodsStatus());
        ret.setPrice(g.getPrice());
        ret.setSupply(g.getSupply());
        ret.setInventory(g.getInventory());
        ret.setGoodsProperty(JSON.parseObject(g.getGoodsProperty(), GoodsInfo.GoodsProperty.class));
        ret.setCreateTime(g.getCreateTime());
        ret.setCreateUser(g.getCreateUser());
        ret.setUpdateTime(g.getUpdateTime());
        ret.setUpdateUser(g.getUpdateUser());
        return ret;
    }

    public static SimpleGoodsInfo toSimple(EcommerceGoods g){
        SimpleGoodsInfo ret = new SimpleGoodsInfo();
        ret.setId(g.getId());
        ret.setGoodsName(g.getGoodsName());
        ret.setGoodsPic(g.getGoodsPic());
        ret.setGoodsDescription(g.getGoodsDescription());
        ret.setPrice(g.getPrice());
        return ret;
    }
}
